package com.ruoyi.web.controller.chargeback.controller;


import java.util.List;
import java.util.Arrays;

import com.ruoyi.charge.bo.EquipmentBatchAddBo;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.charge.vo.EquipmentVo;
import com.ruoyi.charge.bo.EquipmentQueryBo;
import com.ruoyi.charge.bo.EquipmentAddBo;
import com.ruoyi.charge.bo.EquipmentEditBo;
import com.ruoyi.charge.service.IEquipmentService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 充电设备管理Controller
 *
 * @author fanqie
 * @date 2021-10-28
 */
@Api(value = "充电设备管理控制器", tags = {"后台充电设备管理管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/back/equipment")
public class BackEquipmentController extends BaseController {

    private final IEquipmentService iEquipmentService;

    /**
     * 查询充电设备管理列表
     */
    @ApiOperation("查询充电设备管理列表")
    @PreAuthorize("@ss.hasPermi('back:equipment:list')")
    @GetMapping("/list")
    public TableDataInfo<EquipmentVo> list(EquipmentQueryBo bo) {
        startPage();
        List<EquipmentVo> list = iEquipmentService.queryList(bo);
        return getDataTable(list);
    }

    /**
     * 导出充电设备管理列表
     */
    @ApiOperation("导出充电设备管理列表")
    @PreAuthorize("@ss.hasPermi('back:equipment:export')")
    @Log(title = "充电设备管理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult<EquipmentVo> export(EquipmentQueryBo bo) {
        List<EquipmentVo> list = iEquipmentService.queryList(bo);
        ExcelUtil<EquipmentVo> util = new ExcelUtil<EquipmentVo>(EquipmentVo.class);
        return util.exportExcel(list, "充电设备管理");
    }

    /**
     * 获取充电设备管理详细信息
     */
    @ApiOperation("获取充电设备管理详细信息")
    @PreAuthorize("@ss.hasPermi('back:equipment:query')")
    @GetMapping("/{equipmentId}")
    public AjaxResult<EquipmentVo> getInfo(@PathVariable("equipmentId") Long equipmentId) {
        return AjaxResult.success(iEquipmentService.queryById(equipmentId));
    }

    /**
     * 新增充电设备管理
     */
    @ApiOperation("新增充电设备管理")
    @PreAuthorize("@ss.hasPermi('back:equipment:add')")
    @Log(title = "充电设备管理", businessType = BusinessType.INSERT)
    @PostMapping()
    public AjaxResult<Void> add(@RequestBody EquipmentAddBo bo) {
        return toAjax(iEquipmentService.insertByAddBo(bo) ? 1 : 0);
    }

    /**
     * 批量添加充电设备
     */
    @ApiOperation("批量添加充电设备")
    @Log(title = "充电设备管理", businessType = BusinessType.INSERT)
    @PostMapping("/batch/add")
    public AjaxResult<Void> batchAdd(@RequestBody EquipmentBatchAddBo bo){
        return toAjax(iEquipmentService.batchInsert(bo));
    }

    /**
     * 分配充电设备给店铺
     */
    @ApiOperation("分配充电设备给店铺")
    @Log(title = "充电设备管理", businessType = BusinessType.INSERT)
    @GetMapping("/distribute")
    public AjaxResult<Void> distributeEquipment(@RequestParam("equipmentId") Long equipmentId,
                                                @RequestParam("storeId") Long storeId,
                                                @RequestParam("tableNumber") String tableNumber){
        return toAjax(iEquipmentService.distributeEquipment(equipmentId, storeId,tableNumber));
    }

    /**
     * 修改充电设备管理
     */
    @ApiOperation("修改充电设备管理")
    @PreAuthorize("@ss.hasPermi('back:equipment:edit')")
    @Log(title = "充电设备管理", businessType = BusinessType.UPDATE)
    @PutMapping()
    public AjaxResult<Void> edit(@RequestBody EquipmentEditBo bo) {
        return toAjax(iEquipmentService.updateByEditBo(bo) ? 1 : 0);
    }

    /**
     * 删除充电设备管理
     */
    @ApiOperation("删除充电设备管理")
    @PreAuthorize("@ss.hasPermi('back:equipment:remove')")
    @Log(title = "充电设备管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{equipmentIds}")
    public AjaxResult<Void> remove(@PathVariable Long[] equipmentIds) {
        return toAjax(iEquipmentService.deleteWithValidByIds(Arrays.asList(equipmentIds), true) ? 1 : 0);
    }
}
